<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Searches for an element with a certain id</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="domdocument.createtextnode.html">DOMDocument::createTextNode</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="domdocument.getelementsbytagname.html">DOMDocument::getElementsByTagName</a></div>
 <div class="up"><a href="class.domdocument.html">DOMDocument</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="domdocument.getelementbyid" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DOMDocument::getElementById</h1>
  <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">DOMDocument::getElementById</span> &mdash; <span class="dc-title">Searches for an element with a certain id</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-domdocument.getelementbyid-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="class.domelement.html" class="type DOMElement">DOMElement</a></span> <span class="methodname"><strong>DOMDocument::getElementById</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$elementId</code></span>
   )</div>

  <p class="para rdfs-comment">
   This function is similar to
   <a href="domdocument.getelementsbytagname.html" class="xref">DOMDocument::getElementsByTagName</a> but 
   searches for an element with a given id.
  </p>
  <p class="para">
   For this function to work, you will need either to set some ID attributes
   with <a href="domelement.setidattribute.html" class="xref">DOMElement::setIdAttribute</a>
   or a DTD which defines an attribute to be of type ID. 
   In the later case, you will need to validate your document with
   <a href="domdocument.validate.html" class="xref">DOMDocument::validate</a> 
   or <a href="class.domdocument.html#domdocument.props.validateonparse" class="link">DOMDocument::$validateOnParse</a> before using this function.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-domdocument.getelementbyid-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">elementId</code></em></dt>

     <dd>

      <p class="para">
       The unique id value for an element.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-domdocument.getelementbyid-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the <a href="class.domelement.html" class="classname">DOMElement</a> or <strong><code>NULL</code></strong> if the element is
   not found. 
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-domdocument.getelementbyid-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-5283">
   <p><strong>Example #1 DOMDocument::getElementById() Example</strong></p>
   <div class="example-contents"><p>以下范例使用了 <var class="filename">book.xml</var>，其内容如下：</p></div>
<div class="example-contents">
 
<div class="xmlcode"><pre class="xmlcode">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE books [
  &lt;!ELEMENT books   (book+)&gt;
  &lt;!ELEMENT book    (title, author+, xhtml:blurb?)&gt;
  &lt;!ELEMENT title   (#PCDATA)&gt;
  &lt;!ELEMENT blurb   (#PCDATA)&gt;
  &lt;!ELEMENT author  (#PCDATA)&gt;
  &lt;!ATTLIST books   xmlns        CDATA  #IMPLIED&gt;
  &lt;!ATTLIST books   xmlns:xhtml  CDATA  #IMPLIED&gt;
  &lt;!ATTLIST book    id           ID     #IMPLIED&gt;
  &lt;!ATTLIST author  email        CDATA  #IMPLIED&gt;
]&gt;
&lt;?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;style.xsl&quot;?&gt;
&lt;books xmlns=&quot;http://books.php/&quot; xmlns:xhtml=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
  &lt;book id=&quot;php-basics&quot;&gt;
    &lt;title&gt;PHP Basics&lt;/title&gt;
    &lt;author email=&quot;jim.smith@basics.php&quot;&gt;Jim Smith&lt;/author&gt;
    &lt;author email=&quot;jane.smith@basics.php&quot;&gt;Jane Smith&lt;/author&gt;
    &lt;xhtml:blurb&gt;&lt;![CDATA[
&lt;p&gt;&lt;em&gt;PHP Basics&lt;/em&gt; provides an introduction to PHP.&lt;/p&gt;
]]&gt;&lt;/xhtml:blurb&gt;
  &lt;/book&gt;
  &lt;book id=&quot;php-advanced&quot;&gt;
    &lt;title&gt;PHP Advanced Programming&lt;/title&gt;
    &lt;author email=&quot;jon.doe@advanced.php&quot;&gt;Jon Doe&lt;/author&gt;
  &lt;/book&gt;
&lt;/books&gt;</pre>
</div></div>

   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DomDocument</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;We&nbsp;need&nbsp;to&nbsp;validate&nbsp;our&nbsp;document&nbsp;before&nbsp;refering&nbsp;to&nbsp;the&nbsp;id<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">validateOnParse&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Load</span><span style="color: #007700">(</span><span style="color: #DD0000">'book.xml'</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;element&nbsp;whose&nbsp;id&nbsp;is&nbsp;'php-basics'&nbsp;is:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementById</span><span style="color: #007700">(</span><span style="color: #DD0000">'php-basics'</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">tagName&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
The element whose id is &#039;php-basics&#039; is: book 
</pre></div>
   </div>
  </div>
 </div>

 <div class="refsect1 seealso" id="refsect1-domdocument.getelementbyid-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="domdocument.getelementsbytagname.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::getElementsByTagName()</a> - Searches for all elements with given local tag name</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="domdocument.createtextnode.html">DOMDocument::createTextNode</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="domdocument.getelementsbytagname.html">DOMDocument::getElementsByTagName</a></div>
 <div class="up"><a href="class.domdocument.html">DOMDocument</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
